﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTSWMILibrary;
using System.Management;
using Microsoft.BizTalk.ExplorerOM;

namespace BizTalkConsoleObjects
{
    public static class BizTalkWebConsoleActions
    {
        static string ServerName = ".";

        //Overriden in ASP.NET project and assinged from Web.config
        static string BizTalkMgmtDBName = "BizTalkMgmtDb";

        public static void SetConnections(string BizTalkMgmtDB, string serverName)
        {
            ServerName = serverName;
            BizTalkMgmtDBName = BizTalkMgmtDB;
        }

        public static class OrchestrationActions
        {

            public static List<Actions> GetOrchestrationAction(string Orchestration, string ApplicationName)
            {
                OrchestrationStatus orchestrationStatus = BizTalkWebConsoleOM.GetOrchestrationStatus(Orchestration, ApplicationName);

                List<Actions> actionsList = new List<Actions>();

                if (orchestrationStatus == OrchestrationStatus.Started)
                {
                    actionsList.Add(new Actions("Start", false));
                    actionsList.Add(new Actions("Stop", true));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", false));

                }
                else if (orchestrationStatus == OrchestrationStatus.Enlisted)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", false));
                }
                else if (orchestrationStatus == OrchestrationStatus.Unenlisted)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", true));
                    actionsList.Add(new Actions("Unenlist", false));
                    //actionsList.Add(new Actions("Remove", true));
                }


                return actionsList;
            }

            public static void SetOrchestrationStatus(string Orchestration, string Application, OrchestrationStatus Status)
            {
                // Create the root object and set the connection string
                BtsCatalogExplorer catalog = new BtsCatalogExplorer();
                catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);

                try
                {
                    // Set the orchestration status
                    catalog.Applications[Application].Orchestrations[Orchestration].Status = Status;
              
                    // Commit the changes
                    catalog.SaveChanges();
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }
            }


        }

        public static class ReceiveLocationActions
        {
            public static List<Actions> GetReceiveLocationAction(string ReceiveLocation, string ReceivePort, string ApplicationName)
            {
                bool receiveLocationEnabled = BizTalkWebConsoleOM.GetReceiveLocationStatus(ReceiveLocation, ReceivePort, ApplicationName);

                List<Actions> actionsList = new List<Actions>();

                if (receiveLocationEnabled)
                {
                    actionsList.Add(new Actions("Enable", false));
                    actionsList.Add(new Actions("Disable", true));
                }
                else
                {
                    actionsList.Add(new Actions("Enable", true));
                    actionsList.Add(new Actions("Disable", false));
                }

                return actionsList;
            }

            public static void SetReceiveLocationStatus(string ReceiveLocation, string ReceivePort, string ApplicationName, bool Status)
            {
                BtsCatalogExplorer catalog = new BtsCatalogExplorer();
                catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);

                try
                {
                    // Set the orchestration status
                    catalog.Applications[ApplicationName].ReceivePorts[ReceivePort].ReceiveLocations[ReceiveLocation].Enable = Status;

                    // Commit the changes
                    catalog.SaveChanges();
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }
            }
        }

        public static class SendPortActions
        {
            public static List<Actions> GetSendPortAction(string SendPort, string ApplicationName)
            {
                PortStatus portStatus = BizTalkWebConsoleOM.GetSendPortStatus(SendPort, ApplicationName);

                List<Actions> actionsList = new List<Actions>();

                if (portStatus == PortStatus.Started)
                {
                    actionsList.Add(new Actions("Start", false));
                    actionsList.Add(new Actions("Stop", true));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", false));

                }
                else if (portStatus == PortStatus.Bound)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", true));
                    actionsList.Add(new Actions("Unenlist", false));
                    //actionsList.Add(new Actions("Remove", false));
                }
                else if (portStatus == PortStatus.Stopped)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", true));
                }


                return actionsList;
            }

            public static void SetSendPortStatus(string SendPort, string ApplicationName, PortStatus Status)
            {
                BtsCatalogExplorer catalog = new BtsCatalogExplorer();
                catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);

                try
                {
                    // Set the orchestration status
                    catalog.Applications[ApplicationName].SendPorts[SendPort].Status = Status;

                    // Commit the changes
                    catalog.SaveChanges();
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }
            }
        }

        public static class SendPortGroupsActions
        {
            public static List<Actions> GetSendPortGroupsAction(string SendPortGroups, string ApplicationName)
            {
                PortStatus portStatus = BizTalkWebConsoleOM.GetSendPortGroupsStatus(SendPortGroups, ApplicationName);

                List<Actions> actionsList = new List<Actions>();

                if (portStatus == PortStatus.Started)
                {
                    actionsList.Add(new Actions("Start", false));
                    actionsList.Add(new Actions("Stop", true));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", false));

                }
                else if (portStatus == PortStatus.Bound)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", true));
                    actionsList.Add(new Actions("Unenlist", false));
                    //actionsList.Add(new Actions("Remove", false));
                }
                else if (portStatus == PortStatus.Stopped)
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Enlist", false));
                    actionsList.Add(new Actions("Unenlist", true));
                    //actionsList.Add(new Actions("Remove", true));
                }


                return actionsList;
            }

            public static void SetSendPortGroupsStatus(string SendPortGroups, string ApplicationName, PortStatus Status)
            {
                BtsCatalogExplorer catalog = new BtsCatalogExplorer();
                catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);

                try
                {
                    // Set the orchestration status
                    catalog.Applications[ApplicationName].SendPortGroups[SendPortGroups].Status = Status;

                    // Commit the changes
                    catalog.SaveChanges();
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }
            }
        }

        public static class HostInstancesActions
        {
            public static List<Actions> GetHostInstanceAction(string HostInstanceName)
            {
                string HostInstanceStatus = BizTalkWebConsoleOM.GetHostInstanceStatus(HostInstanceName, ServerName);

                List<Actions> actionsList = new List<Actions>();

               if (HostInstanceStatus == "Running")
                {
                    actionsList.Add(new Actions("Start", false));
                    actionsList.Add(new Actions("Stop", true));
                    actionsList.Add(new Actions("Restart", true));
                }
               else if (HostInstanceStatus == "Stopped")
                {
                    actionsList.Add(new Actions("Start", true));
                    actionsList.Add(new Actions("Stop", false));
                    actionsList.Add(new Actions("Restart", false));
                }
               if (HostInstanceStatus == "Not Applicable")
               {
                   actionsList.Add(new Actions("Start", false));
                   actionsList.Add(new Actions("Stop", false));
                   actionsList.Add(new Actions("Restart", false));
               }

                return actionsList;
            }

            public static void StartHostInstance(string HostInstanceName)
            {
                ManagementObject classInstance =
                        new ManagementObject("root\\MicrosoftBizTalkServer",
                        "MSBTS_HostInstance.MgmtDbNameOverride='',MgmtDbServerOverride='',Name='Microsoft BizTalk Server " + HostInstanceName + " " + ServerName + "'",
                        null);
                
                
                    // Execute the method and obtain the return values.
                    ManagementBaseObject outParams =
                        classInstance.InvokeMethod("Start", null, null);


                
            }

            public static void StopHostInstance(string HostInstanceName)
            {
                ManagementObject classInstance =
                        new ManagementObject("root\\MicrosoftBizTalkServer",
                        "MSBTS_HostInstance.MgmtDbNameOverride='',MgmtDbServerOverride='',Name='Microsoft BizTalk Server " + HostInstanceName + " " + ServerName + "'",
                        null);


                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("Stop", null, null);
            }

            public static void RestartHostInstance(string HostInstanceName)
            {
                StopHostInstance(HostInstanceName);
                StartHostInstance(HostInstanceName);
            }
        }
    }
}
